//
//  UIScrollView+MJ.m
//  VICategory
//
//  Created by MN on 2022/7/13.
//

#import "UIScrollView+MJ.h"
#import "MJRefresh.h"

@implementation UIScrollView (MJ)
/// 下拉刷新
/// @param pullDownBlock 下拉block
- (void)vi_pullDown:(void (^)(void))pullDownBlock{
    self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        pullDownBlock();
    }];
}

/// 上拉刷新
/// @param pullUpBlock 上拉block
- (void)vi_pullUp:(void (^)(void))pullUpBlock{
    self.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        pullUpBlock();
    }];
}

/// 上、下拉刷新
/// @param pullDownBlock 下拉block
/// @param pullUpBlock 上拉block
- (void)vi_pullDown:(void (^)(void))pullDownBlock
             pullUp:(void (^)(void))pullUpBlock{
    [self vi_pullDown:pullDownBlock];
    [self vi_pullUp:pullUpBlock];
}

/// 头视图开始刷新
- (void)vi_headerBeginRefreshing{
    [self.mj_header beginRefreshing];
}

///  脚视图开始刷新
- (void)vi_footerBeginRefreshing{
    [self.mj_footer beginRefreshing];
}

/// 头视图停止刷新
- (void)vi_headerEndRefreshing{
    [self.mj_header endRefreshing];
}

/// 脚视图停止刷新
- (void)vi_footerEndRefreshing{
    [self.mj_footer endRefreshing];
}

/// 都停止刷新
- (void)vi_allEndRefreshing{
    [self vi_headerEndRefreshing];
    [self vi_footerEndRefreshing];
}

- (void)vi_registerOneClass:(NSString *)className{
    if (!className) return;
    [self vi_registerClass:@[className]];
}
- (void)vi_registerClass:(NSArray <NSString *> *)classNames{
    if ([self isKindOfClass:UITableView.class]) {
        for (NSString *className in classNames) {
            UITableView *table = (UITableView *)self;
            [table registerClass:NSClassFromString(className) forCellReuseIdentifier:className];
        }
    }else if ([self isKindOfClass:UICollectionView.class]) {
        for (NSString *className in classNames) {
            UICollectionView *collectionView = (UICollectionView *)self;
            [collectionView registerClass:NSClassFromString(className) forCellWithReuseIdentifier:className];
        }
    }
}

- (void)vi_registerOneNib:(NSString *)nibName{
    if (!nibName) return;
    [self vi_registerNib:@[nibName]];
}
- (void)vi_registerNib:(NSArray <NSString *> *)nibNames{
    if ([self isKindOfClass:UITableView.class]) {
        for (NSString *nibName in nibNames) {
            UITableView *table = (UITableView *)self;
            [table registerNib:[UINib nibWithNibName:nibName bundle:NSBundle.mainBundle] forCellReuseIdentifier:nibName];
        }
    }else if ([self isKindOfClass:UICollectionView.class]) {
        for (NSString *nibName in nibNames) {
            UICollectionView *collectionView = (UICollectionView *)self;
            [collectionView registerNib:[UINib nibWithNibName:nibName bundle:NSBundle.mainBundle] forCellWithReuseIdentifier:nibName];
        }
    }
}
@end
